﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsTaskManager
{
    public partial class MainForm : Form
    {
        private TaskManager _taskManager;
        private string _dataFilePath = "tasks.xml";

        public MainForm()
        {
            InitializeComponent();
            _taskManager = new TaskManager();

            // 初始化优先级下拉框
            cmbPriority.Items.AddRange(new string[] { "低", "中", "高" });
            cmbPriority.SelectedIndex = 1; // 默认选中"中"

            // 加载任务数据
            LoadTasks();
        }

        // 加载任务
        private void LoadTasks()
        {
            bool loadSuccess = _taskManager.LoadTasks(_dataFilePath);
            if (loadSuccess)
            {
                UpdateTaskList();
                lblStatus.Text = "任务已加载";
            }
            else
            {
                lblStatus.Text = "未找到保存的任务";
            }
        }

        // 保存任务
        private void SaveTasks()
        {
            bool saveSuccess = _taskManager.SaveTasks(_dataFilePath);
            lblStatus.Text = saveSuccess ? "任务已保存" : "保存失败";
        }

        // 更新任务列表显示
        private void UpdateTaskList()
        {
            List<Task> tasks = string.IsNullOrWhiteSpace(txtSearch.Text)
                ? _taskManager.GetAllTasks()
                : _taskManager.SearchTasks(txtSearch.Text);

            lstTasks.Items.Clear();

            foreach (var task in tasks)
            {
                string status = task.IsCompleted ? "✓" : "○";
                string itemText = $"{task.Id}. [{status}] {task.Title} - 截止: {task.DueDate:yyyy-MM-dd} - 优先级: {task.Priority}";

                ListViewItem item = new ListViewItem(itemText);
                item.Tag = task; // 存储任务对象引用
                item.ForeColor = task.IsCompleted ? System.Drawing.Color.Gray : System.Drawing.Color.Black;
                lstTasks.Items.Add(item);
            }
        }

        // 添加任务按钮
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtTitle.Text))
            {
                MessageBox.Show("请输入任务标题", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Task newTask = new Task
            {
                Title = txtTitle.Text,
                Description = txtDescription.Text,
                DueDate = dtpDueDate.Value,
                Priority = cmbPriority.SelectedItem.ToString(),
                IsCompleted = false
            };

            _taskManager.AddTask(newTask);
            SaveTasks();
            UpdateTaskList();

            // 清空输入框
            txtTitle.Clear();
            txtDescription.Clear();
        }

        // 标记完成按钮
        private void btnComplete_Click(object sender, EventArgs e)
        {
            if (lstTasks.SelectedItems.Count > 0)
            {
                Task selectedTask = (Task)lstTasks.SelectedItems[0].Tag;
                _taskManager.MarkTaskAsCompleted(selectedTask.Id);
                SaveTasks();
                UpdateTaskList();
            }
            else
            {
                MessageBox.Show("请选择一个任务", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // 搜索文本变化时
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            UpdateTaskList();
        }

        // 显示任务详情
        private void lstTasks_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTasks.SelectedItems.Count > 0)
            {
                Task selectedTask = (Task)lstTasks.SelectedItems[0].Tag;
                txtDetails.Text = $"ID: {selectedTask.Id}\r\n" +
                                 $"标题: {selectedTask.Title}\r\n" +
                                 $"描述: {selectedTask.Description}\r\n" +
                                 $"截止日期: {selectedTask.DueDate:yyyy-MM-dd}\r\n" +
                                 $"优先级: {selectedTask.Priority}\r\n" +
                                 $"状态: {(selectedTask.IsCompleted ? "已完成" : "未完成")}";
            }
        }

        // 删除任务按钮
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstTasks.SelectedItems.Count > 0)
            {
                Task selectedTask = (Task)lstTasks.SelectedItems[0].Tag;
                DialogResult result = MessageBox.Show(
                    $"确定要删除任务 '{selectedTask.Title}' 吗?",
                    "确认删除",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    _taskManager.DeleteTask(selectedTask.Id);
                    SaveTasks();
                    UpdateTaskList();
                    txtDetails.Clear();
                }
            }
            else
            {
                MessageBox.Show("请选择一个任务", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // 窗体关闭时保存任务
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveTasks();
        }

        private void txtTitle_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
